import {
  Body,
  Controller,
  Get,
  Post,
  Render,
  UploadedFile,
  UseInterceptors,
} from '@nestjs/common';
import { FileInterceptor } from '@nestjs/platform-express';
import { createWriteStream } from 'fs';
import { join } from 'path';

@Controller('upload')
export class UploadController {
  /**
   * form表单
   */
  @Get()
  @Render('default/upload')
  index() {
    return '';
  }

  /**
   * doAdd
   */
  @Post('doAdd')
  @UseInterceptors(FileInterceptor('pic'))
  doAdd(@Body() post, @UploadedFile() file) {
    const writeImage = createWriteStream(
      join(__dirname, '..', '..', 'public/upload', `${file.originalname}`),
    );
    writeImage.write(file.buffer);
    return '上传成功！';
  }
}
